package vn.lumi.smarthome.ui;

import vn.lumi.smarthome.R;
import vn.lumi.smarthome.io.database.TBL_Scene;
import vn.lumi.smarthome.ulti.LogUltis;
import android.content.Context;
import android.database.Cursor;
import android.os.Bundle;
import android.support.v4.app.LoaderManager.LoaderCallbacks;
import android.support.v4.content.CursorLoader;
import android.support.v4.content.Loader;
import android.support.v4.widget.CursorAdapter;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.CheckBox;
import android.widget.ListView;
import android.widget.TextView;

public class SceneFragment extends BaseFragment implements LoaderCallbacks<Cursor>{
	private static final String TAG="SceneFragment";
	
	private ListView mListScenes;
	private SceneAdapter mSceneAdapter;
	
	private static SceneFragment mSceneFragment;
	//Khai bao ID cua Loader
	private static final int LOADER_ID=1;
	
	public SceneFragment() {
		LogUltis.i(TAG, "create SceneFragment");
	}
	public static SceneFragment newInstance(int floor_id,int room_id){
		if(mSceneFragment==null){
			mSceneFragment=new SceneFragment();
		}
		Bundle bundle=new Bundle();
		bundle.putInt(FLOOR_ID, floor_id);
		bundle.putInt(ROOM_ID, room_id);
		mSceneFragment.setArguments(bundle);
		return mSceneFragment;
	}
	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		mFloor_id=getArguments().getInt(FLOOR_ID);
		mRoom_id=getArguments().getInt(ROOM_ID);
	}
	
	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container,
			Bundle savedInstanceState) {
		View view=inflater.inflate(R.layout.fragment_scene, container, false);
		init_view(view);
		
		return view;
	}
	
	private void init_view(View view){
		mListScenes=(ListView)view.findViewById(R.id.fragment_scene_list_scene);
		
		mSceneAdapter=new SceneAdapter(getActivity(), null, CursorAdapter.FLAG_REGISTER_CONTENT_OBSERVER);
		mListScenes.setAdapter(mSceneAdapter);
	}
	
	@Override
	public void onResume() {
		super.onResume();
		//load data
//		getLoaderManager().restartLoader(0, null, this);
		refreshData(mFloor_id, mRoom_id);
	}
	@Override
	public void refreshData(int floor_id,int room_id){
		mFloor_id=floor_id;
		mRoom_id=room_id;
		Bundle bundle=new Bundle();
		bundle.putInt(FLOOR_ID, mFloor_id);
		bundle.putInt(ROOM_ID, mRoom_id);
		getLoaderManager().restartLoader(LOADER_ID, bundle, this);
		mSceneAdapter.swapCursor(null);
	}
	
	
	@Override
	public Loader<Cursor> onCreateLoader(int id, Bundle bundle) {
//		int floor_id=bundle.getInt(FLOOR_ID);
		int room_id=bundle.getInt(ROOM_ID);
		String selection=TBL_Scene.ROOM_ID+"=?";
		String[] selectionArgs=new String[]{room_id+""};
		return new CursorLoader(getActivity(), TBL_Scene.CONTENT_URI_SCENE, null, selection, selectionArgs, null);
	}
	@Override
	public void onLoadFinished(Loader<Cursor> arg0, Cursor arg1) {
		mSceneAdapter.swapCursor(arg1);
	}
	@Override
	public void onLoaderReset(Loader<Cursor> arg0) {
		mSceneAdapter.swapCursor(null);
	}
	
	private static class SceneAdapter extends CursorAdapter{
		private static final String TAG="SceneAdapter";
		
		public SceneAdapter(Context context, Cursor c, int flags) {
			super(context, c, flags);
		}

		@Override
		public void bindView(View view, Context context, Cursor cursor) {
			TextView txtName=(TextView)view.findViewById(R.id.item_listview_scene_active_txt_name);
			CheckBox chbActive=(CheckBox)view.findViewById(R.id.item_listview_scene_active_ckb_active);
			String name=cursor.getString(cursor.getColumnIndex(TBL_Scene.NAME));
			int state=cursor.getInt(cursor.getColumnIndex(TBL_Scene.STATE));
			if(state==0){
				chbActive.setChecked(false);
			}else{
				chbActive.setChecked(true);
			}
			txtName.setText(name);
		}

		@Override
		public View newView(Context context, Cursor cursor, ViewGroup viewgroup) {
			return View.inflate(context, R.layout.item_listview_scene_active, null);
		}
		
	}
}
